﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

public class MaterialShaderChanger : MonoBehaviour
{
    public Renderer[] renderers;

    public Shader newShader;
    public List<Shader> oldShaders = new List<Shader>();

    private void Awake()
    {
        renderers = GetComponentsInChildren<Renderer>();
    }

    public void Replace()
    {
        if (renderers == null || renderers.Length == 0)
        {
            return;
        }

        oldShaders.Clear();
        foreach (Renderer render in renderers)
        {
            if (render)
            {
                oldShaders.Add(render.material.shader);
                render.material.shader = newShader;
            }
        }
    }

    public void Reset()
    {
        if (renderers == null || renderers.Length == 0 || oldShaders.Count == 0)
        {
            return;
        }

        for (var i = 0; i < renderers.Length; i++)
        {
            Renderer render = renderers[i];
            Shader oldShader = oldShaders[i];
            if (render)
            {
                render.material.shader = oldShader;
            }
        }
    }
}